﻿using BJoin.Application.WebApi.Controllers;
using BJoin.Common;
using BJoin.MicroSVC.Sample.DataContract.Dtos;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Microsoft.FeatureManagement;
using Swashbuckle.AspNetCore.Annotations;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace BJoin.MicroSVC.Sample.Webapi.Controllers
{
    [ApiVersion("1.0")]
    [Route("api/v{version:apiVersion}/[controller]")]
    [SwaggerTag("枚举接口服务")]
    public class EnumController : BaseController<EnumController>
    {
        public EnumController(ILoggerFactory loggerFactory, IFeatureManagerSnapshot featureManager) : base(loggerFactory, featureManager)
        {
        }

        /// <summary>
        /// 传入枚举获取下拉列表
        /// </summary>
        /// <param name="value">EnumDropDown枚举</param>
        /// <returns>下拉列表</returns>
        /// <response code="200">返回对象集合</response>
        /// <response code="400">参数不正确...</response>
        /// <response code="404">无记录</response>
        /// <response code="500">内部错误...</response>
        [HttpGet("filter-drop-down/{value}")]
        [ProducesResponseType(typeof(string), 400)]
        [ProducesResponseType(typeof(string), 404)]
        [ProducesResponseType(typeof(string), 500)]
        public async Task<IActionResult> FilterDropDownAsync(EnumDropDown value)
        {
            var result = await GetDropdownDictAsync(value);
            return Ok(result);
        }

        /// <summary>
        /// 获取所有的枚举下拉列表
        /// </summary>
        /// <returns>下拉列表对象</returns>
        /// <response code="200">返回所有下拉对象</response>
        /// <response code="400">参数不正确...</response>
        /// <response code="404">无记录</response>
        /// <response code="500">内部错误...</response>
        [HttpGet("all-drop-down")]
        [ProducesResponseType(typeof(string), 400)]
        [ProducesResponseType(typeof(string), 404)]
        [ProducesResponseType(typeof(string), 500)]
        public async Task<IActionResult> FilterAllDropDownAsync()
        {
            var result = new Dictionary<string, IEnumerable<object>>();
            result = new Dictionary<string, IEnumerable<object>>();
            await Enum.GetNames(typeof(EnumDropDown)).ToList().ForEachAsync(async e =>
            {
                var downEnum = (EnumDropDown)Enum.Parse(typeof(EnumDropDown), e);
                var dictEnum = await GetDropdownDictAsync(downEnum);
                result.Add(e, dictEnum);
            });
            return Ok(result);
        }

        [NonAction]
        public async static Task<IEnumerable<object>> GetDropdownDictAsync(EnumDropDown value)
        {
            var refEnumType = EnumHelper.GetEnumAttribute<EnumDropDown, ReferenceEnumTypeAttribute>(value);
            if (refEnumType != null)
            {
                var dictEnum = EnumHelper.ConvertEnumToList<int>(refEnumType.EnumType);
                return dictEnum.Select(o => new { o.Key, o.Value }).ToList();
            }
            var result = new List<object>();
            var dropDownStr = EnumHelper.GetEnumDescription(value);
            dropDownStr.Split(';').ToList().ForEach(item =>
            {
                if (!string.IsNullOrEmpty(item))
                {
                    var kv = item.Split(':');
                    if (kv.Length == 2) result.Add(new { Key = kv[0], Value = kv[1] });
                }
            });

            return await Task.FromResult(result);
        }
    }
}
